package com.letv.core.db;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.Executors;

import com.letv.core.bean.DownloadDBListBean.DownloadDBBean;
import com.letv.core.bean.DownloadDBListBean;
import com.letv.core.constant.DatabaseConstant;
import com.letv.core.constant.DownloadConstant;
import com.letv.core.constant.LetvConstant;
import com.letv.core.utils.DownloadUtils;
import com.letv.core.utils.LetvTools;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;

public class DownloadTraceHandler {

    private Context mContext;

    public DownloadTraceHandler(Context context) {
        mContext = context;
    }

    /**
     * 创建一个下载记录 或者 根据本地缓存数据，更新数据库
     * 
     * @param DownloadDBBean
     * @return
     */
    public boolean saveDownloadTrace(DownloadDBBean bean) {

        if (has(bean.getVid()) != null) {
            return updateByEpisodeId(bean);
        }
        ContentValues cv = new ContentValues();
        cv.put(DatabaseConstant.DownloadTrace.Field.DURATION, bean.getDuration());
        cv.put(DatabaseConstant.DownloadTrace.Field.EPISODE_ID, bean.getVid());
        cv.put(DatabaseConstant.DownloadTrace.Field.ALBUM_ID, bean.getAid());
        cv.put(DatabaseConstant.DownloadTrace.Field.ICON, bean.getIcon());
        cv.put(DatabaseConstant.DownloadTrace.Field.TYPE, bean.getType());
        cv.put(DatabaseConstant.DownloadTrace.Field.ORDER, bean.getOrd());
        cv.put(DatabaseConstant.DownloadTrace.Field.CID, bean.getCid());
        cv.put(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE,
                bean.getEpisodetitle());
        cv.put(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON,
                bean.getEpisodeIcon());
        cv.put(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE,
                bean.getAlbumtitle());
        cv.put(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE, bean.getTotalsize());
        cv.put(DatabaseConstant.DownloadTrace.Field.FINISH, bean.getFinish());
        cv.put(DatabaseConstant.DownloadTrace.Field.TIMESTAMP, System.currentTimeMillis());
        cv.put(DatabaseConstant.DownloadTrace.Field.LENGTH, bean.getLength());

        cv.put(DatabaseConstant.DownloadTrace.Field.FILE_PATH, TextUtils
                .isEmpty(bean.getFilePath()) ? DownloadConstant.DOWNLOAD_LOCATION_DIR
                : bean.getFilePath());
        cv.put(DatabaseConstant.DownloadTrace.Field.ISHD, bean.getIsHd());
        cv.put(DatabaseConstant.DownloadTrace.Field.IS_NEW, bean.getIsNew());
        cv.put(DatabaseConstant.DownloadTrace.Field.B_TIME, bean.getBtime());
        cv.put(DatabaseConstant.DownloadTrace.Field.E_TIME, bean.getEtime());
        cv.put(DatabaseConstant.DownloadTrace.Field.IS_WATCH, bean.getIsWatch());
        cv.put(DatabaseConstant.DownloadTrace.Field.DURATION, bean.getDuration());
        
        mContext.getContentResolver().insert(LetvContentProvider.URI_DOWNLOADTRACE, cv);
        return true;
    }

    /**
     * 根据 EPISODE_ID 更新数据表
     */
    private boolean updateByEpisodeId(DownloadDBBean mDownloadDBBean) {
        try {
            if (mDownloadDBBean != null) {
                ContentValues cv = new ContentValues();
                cv.put(DatabaseConstant.DownloadTrace.Field.EPISODE_ID,
                        mDownloadDBBean.getVid());
                cv.put(DatabaseConstant.DownloadTrace.Field.ALBUM_ID,
                        mDownloadDBBean.getAid());
                cv.put(DatabaseConstant.DownloadTrace.Field.ICON, mDownloadDBBean.getIcon());
                cv.put(DatabaseConstant.DownloadTrace.Field.TYPE, mDownloadDBBean.getType());
                cv.put(DatabaseConstant.DownloadTrace.Field.ORDER, mDownloadDBBean.getOrd());
                cv.put(DatabaseConstant.DownloadTrace.Field.CID, mDownloadDBBean.getCid());
                cv.put(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE,
                        mDownloadDBBean.getEpisodetitle());
                cv.put(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON,
                        mDownloadDBBean.getEpisodeIcon());
                cv.put(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE,
                        mDownloadDBBean.getAlbumtitle());
                cv.put(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE,
                        mDownloadDBBean.getTotalsize());
                cv.put(DatabaseConstant.DownloadTrace.Field.FINISH,
                        mDownloadDBBean.getFinish());
                cv.put(DatabaseConstant.DownloadTrace.Field.TIMESTAMP,
                        System.currentTimeMillis());
                cv.put(DatabaseConstant.DownloadTrace.Field.LENGTH,
                        mDownloadDBBean.getLength());

                cv.put(DatabaseConstant.DownloadTrace.Field.FILE_PATH, TextUtils
                        .isEmpty(mDownloadDBBean.getFilePath()) ? DownloadConstant.DOWNLOAD_LOCATION_DIR
                        : mDownloadDBBean.getFilePath());
                cv.put(DatabaseConstant.DownloadTrace.Field.ISHD, mDownloadDBBean.getIsHd());

                cv.put(DatabaseConstant.DownloadTrace.Field.IS_NEW, mDownloadDBBean.getIsNew());
                cv.put(DatabaseConstant.DownloadTrace.Field.B_TIME, mDownloadDBBean.getBtime());
                cv.put(DatabaseConstant.DownloadTrace.Field.E_TIME, mDownloadDBBean.getEtime());
                cv.put(DatabaseConstant.DownloadTrace.Field.IS_WATCH, mDownloadDBBean.getIsWatch());
                
                mContext.getContentResolver().update(LetvContentProvider.URI_DOWNLOADTRACE, cv,
                        DatabaseConstant.DownloadTrace.Field.EPISODE_ID + "=?",
                        new String[] { mDownloadDBBean.getVid() + "" });
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * xulong add 解决下载完成视频点击观看后置顶不更新时间戳
     * @param mDownloadDBBean
     * @return
     */
    private boolean updateStateByEpisodeId(DownloadDBBean mDownloadDBBean) {
        try {
            if (mDownloadDBBean != null) {
                ContentValues cv = new ContentValues();
                cv.put(DatabaseConstant.DownloadTrace.Field.EPISODE_ID,
                        mDownloadDBBean.getVid());
                cv.put(DatabaseConstant.DownloadTrace.Field.ALBUM_ID,
                        mDownloadDBBean.getAid());
                cv.put(DatabaseConstant.DownloadTrace.Field.ICON, mDownloadDBBean.getIcon());
                cv.put(DatabaseConstant.DownloadTrace.Field.TYPE, mDownloadDBBean.getType());
                cv.put(DatabaseConstant.DownloadTrace.Field.ORDER, mDownloadDBBean.getOrd());
                cv.put(DatabaseConstant.DownloadTrace.Field.CID, mDownloadDBBean.getCid());
                cv.put(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE,
                        mDownloadDBBean.getEpisodetitle());
                cv.put(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON,
                        mDownloadDBBean.getEpisodeIcon());
                cv.put(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE,
                        mDownloadDBBean.getAlbumtitle());
                cv.put(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE,
                        mDownloadDBBean.getTotalsize());
                cv.put(DatabaseConstant.DownloadTrace.Field.FINISH,
                        mDownloadDBBean.getFinish());
                cv.put(DatabaseConstant.DownloadTrace.Field.LENGTH,
                        mDownloadDBBean.getLength());

                cv.put(DatabaseConstant.DownloadTrace.Field.FILE_PATH, TextUtils
                        .isEmpty(mDownloadDBBean.getFilePath()) ? DownloadConstant.DOWNLOAD_LOCATION_DIR
                        : mDownloadDBBean.getFilePath());
                cv.put(DatabaseConstant.DownloadTrace.Field.ISHD, mDownloadDBBean.getIsHd());

                cv.put(DatabaseConstant.DownloadTrace.Field.IS_NEW, mDownloadDBBean.getIsNew());
                cv.put(DatabaseConstant.DownloadTrace.Field.B_TIME, mDownloadDBBean.getBtime());
                cv.put(DatabaseConstant.DownloadTrace.Field.E_TIME, mDownloadDBBean.getEtime());
                cv.put(DatabaseConstant.DownloadTrace.Field.IS_WATCH, mDownloadDBBean.getIsWatch());

                mContext.getContentResolver().update(LetvContentProvider.URI_DOWNLOADTRACE, cv,
                        DatabaseConstant.DownloadTrace.Field.EPISODE_ID + "=?",
                        new String[] { mDownloadDBBean.getVid() + "" });
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据下载任务状态获取下载记录数量
     * 
     * @param status
     * @return
     */
    public int getDownloadTraceNumByStatus(int status) {
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.FINISH + "=?",
                new String[] { status+"" },
                DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " ASC");
            return cursor.getCount();
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }

    /**
     * 根据下载任务状态获取下载记录数量
     *
     * @param status
     * @return
     */
    public DownloadDBListBean getDownloadTraceByStatus(int status) {
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.FINISH + "=?",
                    new String[] { status+"" },
                    DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " ASC");
            DownloadDBListBean list = new DownloadDBListBean();

            while (cursor.moveToNext()) {
                DownloadDBListBean.DownloadDBBean bean = new DownloadDBListBean.DownloadDBBean();
                bean.setVid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                bean.setAid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                bean.setIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                bean.setType(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                bean.setOrd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                bean.setCid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                bean.setEpisodetitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                bean.setEpisodeIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                bean.setAlbumtitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                bean.setTotalsize(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                bean.setFinish(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                bean.setTimestamp(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                bean.setLength(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                bean.setFilePath(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                bean.setIsHd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                bean.setIsNew(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                bean.setBtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                bean.setEtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));

                list.add(bean);
            }

            return list;
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }
    
    /**
     * 根据下载任务状态获取除该状态下载记录数量
     * 
     * @param status
     * @return
     */
    public int getDownloadTraceNumExceptStatus(int status) {
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.FINISH + "!=?",
                new String[] { status+"" },
                DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " ASC");
            return cursor.getCount();
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }

    /**
     * 获取当前正在下载的数据
     * @return
     */
    public DownloadDBBean getCurrentDownloadingDBBean() {
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.FINISH + "==",
                    new String[] { "1" },
                    DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " ASC");

            DownloadDBBean downloadDBBean = null;

            while (cursor.moveToNext()) {
                downloadDBBean = new DownloadDBBean();

                downloadDBBean.setVid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                downloadDBBean.setAid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                downloadDBBean.setIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                downloadDBBean.setType(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                downloadDBBean.setOrd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                downloadDBBean.setCid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                downloadDBBean.setEpisodetitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                downloadDBBean.setEpisodeIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                downloadDBBean.setAlbumtitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                downloadDBBean.setTotalsize(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                downloadDBBean.setFinish(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                downloadDBBean.setTimestamp(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                downloadDBBean.setLength(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                downloadDBBean.setFilePath(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                downloadDBBean.setIsHd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                downloadDBBean.setIsNew(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                downloadDBBean.setBtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                downloadDBBean.setEtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
                downloadDBBean.setDuration(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.DURATION)));
            }

            return downloadDBBean;
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }
    
    public boolean isDownloading(){
        try {
            Cursor cursor = mContext.getContentResolver().query(
                    LetvContentProvider.URI_DOWNLOADTRACE,
                    null,
                    DatabaseConstant.DownloadTrace.Field.FINISH + "= ?",
                    new String[] { "1" },
                    DatabaseConstant.DownloadTrace.Field.TIMESTAMP
                            + " ASC");
            if (cursor == null) {
                return false;
            }

            if (cursor.moveToFirst()) {
                cursor.close();
                return true;
            } else {
                cursor.close();
                return false;
            }
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取正在下载列表
     * 
     * @return DownloadDBBeanList
     */
    public DownloadDBListBean getLoadingDownloadInfo() {
        DownloadDBListBean list = new DownloadDBListBean();

        boolean flag = false;

        Cursor cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                null, DatabaseConstant.DownloadTrace.Field.FINISH + "<>?",
                new String[] { "4" }, DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " ASC");
        if (cursor == null){
            return list;
        }
        
        boolean hasUpdateMulti = PreferencesManager.getInstance().hasUpdateMulti();
        if (!hasUpdateMulti) {
            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    int episodeId = cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID));
                    // 比对下载表，下载包实现has方法
//                    boolean isExist = DownloadManager.getInstance(mContext).isDownloadExist(
//                            episodeId + "");
//                    if (!isExist) {
//                        delete(episodeId);
//                        flag = true;
//                    }
                }
            }
            PreferencesManager.getInstance().updateMulti();
        }

        if (flag) {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.FINISH + "<>?",
                    new String[] { "4" },
                    DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " ASC");
        } else {
            cursor.moveToPosition(-1);
        }

        while (cursor.moveToNext()) {
            DownloadDBBean mDownloadDBBean = new DownloadDBBean();

            mDownloadDBBean.setVid(cursor.getInt(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
            mDownloadDBBean.setAid(cursor.getInt(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
            mDownloadDBBean.setIcon(cursor.getString(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
            mDownloadDBBean.setType(cursor.getInt(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
            mDownloadDBBean.setOrd(cursor.getInt(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
            mDownloadDBBean.setCid(cursor.getInt(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
            mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
            mDownloadDBBean.setEpisodeIcon(cursor.getString(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
            mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
            mDownloadDBBean.setTotalsize(cursor.getLong(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
            mDownloadDBBean.setFinish(cursor.getInt(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
            mDownloadDBBean.setTimestamp(cursor.getLong(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
            mDownloadDBBean.setLength(cursor.getLong(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
            mDownloadDBBean.setIsHd(cursor.getInt(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
            mDownloadDBBean.setIsNew(cursor.getInt(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
            mDownloadDBBean.setBtime(cursor.getLong(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
            
            mDownloadDBBean.setEtime(cursor.getLong(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
            mDownloadDBBean.setDuration(cursor.getLong(cursor
                    .getColumnIndex(DatabaseConstant.DownloadTrace.Field.DURATION)));
            list.add(mDownloadDBBean);
        }

        cursor.close();

        return list;

    }
    
    /**
     * 获取未完成列表，除了状态为已完成的列表
     * @return
     */
    public DownloadDBListBean getDownloadExceptFinishTrace() {
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.FINISH + "!=?",
                    new String[] { "4" },
                    DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " ASC");

            DownloadDBListBean arrayList = new DownloadDBListBean();

            while (cursor != null && cursor.moveToNext()) {
                DownloadDBBean mDownloadDBBean = new DownloadDBBean();

                mDownloadDBBean.setVid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                mDownloadDBBean.setAid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                mDownloadDBBean.setIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                mDownloadDBBean.setType(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                mDownloadDBBean.setOrd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                mDownloadDBBean.setCid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                mDownloadDBBean.setEpisodeIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                mDownloadDBBean.setTotalsize(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                mDownloadDBBean.setFinish(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                mDownloadDBBean.setTimestamp(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                mDownloadDBBean.setLength(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                mDownloadDBBean.setFilePath(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                mDownloadDBBean.setIsHd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                mDownloadDBBean.setIsNew(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                mDownloadDBBean.setBtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                mDownloadDBBean.setEtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
                mDownloadDBBean.setDuration(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.DURATION)));
                arrayList.add(mDownloadDBBean);
            }

            return arrayList;
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }

    /**
     * 获取所有已下载完成的数据
     * 
     * @return DownloadDBBeanList
     */
    public DownloadDBListBean getAllFinishTrace() {
        Cursor cursor = null;
        ArrayList<Integer> deList = new ArrayList<Integer>();
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.FINISH + "=?",
                    new String[] { "4" },
                    DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " DESC");

            DownloadDBListBean arrayList = new DownloadDBListBean();

            while (cursor != null && cursor.moveToNext()) {
                DownloadDBBean mDownloadDBBean = new DownloadDBBean();

                mDownloadDBBean.setVid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                mDownloadDBBean.setAid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                mDownloadDBBean.setIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                mDownloadDBBean.setType(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                mDownloadDBBean.setOrd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                mDownloadDBBean.setCid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                mDownloadDBBean.setEpisodeIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                mDownloadDBBean.setTotalsize(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                mDownloadDBBean.setFinish(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                mDownloadDBBean.setTimestamp(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                mDownloadDBBean.setLength(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                mDownloadDBBean.setFilePath(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                mDownloadDBBean.setIsHd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                mDownloadDBBean.setIsNew(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                mDownloadDBBean.setBtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                mDownloadDBBean.setEtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
                 //添加是否观看
                mDownloadDBBean.setIsWatch(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_WATCH)));
                mDownloadDBBean.setDuration(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.DURATION)));
                
                arrayList.add(mDownloadDBBean);
            }
            return arrayList;
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }
    
    /**
     * 获取所有的下载记录的最新一条
     * Added by chenhong on 2014/07/31
     * @return
     */
    public DownloadDBListBean getAllFinishLatestThreeDownLoadTrace() {
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.FINISH + "=?",
                    new String[] { "4" },
                    DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " DESC");

            DownloadDBListBean arrayList = new DownloadDBListBean();
            ArrayList<Integer> albumIdList = new ArrayList<Integer>(); 
            int count = 0;
            while (cursor.moveToNext() && count < 3) {
                DownloadDBBean mDownloadDBBean = new DownloadDBBean();
                int albumId = cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID));
                mDownloadDBBean.setAid(albumId);
                mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                if (!albumIdList.contains(albumId)) {
                    albumIdList.add(albumId);
                    arrayList.add(mDownloadDBBean);
                    count++;
                }
            }
            return arrayList;
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }
    /**
     * 检测文件是否存在
     * @param albumId
     * @param episodeId
     * @param ordId
     * @param isNew
     * @return
     */
    public boolean checkFileExist(int albumId, int episodeId, float ordId, boolean isNew) {
        File file = null;
        if (isNew) {
            file = DownloadUtils.getDownloadFile(episodeId);
        } else {
            file = DownloadUtils.getDownloadFile(albumId, ordId);
        }

        return file != null && file.exists();
    }
    /**
     * 按专辑Id将下载完成的分类
     * @return
     */
    public LinkedHashMap<String, DownloadDBListBean> getAllFinishTraceByAlbumId() {
        LinkedHashMap<String, DownloadDBListBean> mAllDownLoadItemsMap = null;
        DownloadDBListBean mDownloadDBBeanList = null;
        
        try {
            DownloadDBListBean allDownLoadTrace = getAllFinishTrace();
            mAllDownLoadItemsMap = new LinkedHashMap<String, DownloadDBListBean>();
            
        } finally {
        }
        return mAllDownLoadItemsMap;
    }
    
    /**
     * 获取某一专辑Id下所有已完成的数据
     * @param albumId
     * @return
     */
    public DownloadDBListBean getAllFinishTraceByAlbumId(String albumId) {
        DownloadDBListBean mDownloadDBBeanList = new DownloadDBListBean();
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.ALBUM_ID + "=? AND "
                            + DatabaseConstant.DownloadTrace.Field.FINISH + "=?",
                    new String[] { albumId + "", "4" },
                    DatabaseConstant.DownloadTrace.Field.ORDER + " ASC");
            DownloadDBBean mDownloadDBBean = null;
            while (cursor.moveToNext()) {
                mDownloadDBBean = new DownloadDBBean();
                mDownloadDBBean.setVid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                mDownloadDBBean.setAid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                mDownloadDBBean.setIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                mDownloadDBBean.setType(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                mDownloadDBBean.setOrd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                mDownloadDBBean.setCid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                mDownloadDBBean.setEpisodeIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                mDownloadDBBean.setTotalsize(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                mDownloadDBBean.setFinish(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                mDownloadDBBean.setTimestamp(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                mDownloadDBBean.setLength(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                mDownloadDBBean.setFilePath(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                mDownloadDBBean.setIsHd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                mDownloadDBBean.setIsNew(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                mDownloadDBBean.setBtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                mDownloadDBBean.setEtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
                
                mDownloadDBBean.setIsWatch(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_WATCH)));
                mDownloadDBBean.setDuration(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.DURATION)));
                
                
                
                mDownloadDBBeanList.add(mDownloadDBBean);
                
            }
            return mDownloadDBBeanList;
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }

    /**
     * 获取所有的下载记录
     * 
     * @return
     */
    public DownloadDBListBean getAllDownLoadTrace() {
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, null, null, DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " ASC");

            DownloadDBListBean arrayList = new DownloadDBListBean();
            while (cursor.moveToNext()) {
                DownloadDBBean mDownloadDBBean = new DownloadDBBean();

                mDownloadDBBean.setVid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                mDownloadDBBean.setAid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                mDownloadDBBean.setIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                mDownloadDBBean.setType(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                mDownloadDBBean.setOrd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                mDownloadDBBean.setCid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                mDownloadDBBean.setEpisodeIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                mDownloadDBBean.setTotalsize(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                mDownloadDBBean.setFinish(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                mDownloadDBBean.setTimestamp(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                mDownloadDBBean.setLength(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                mDownloadDBBean.setFilePath(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                mDownloadDBBean.setIsHd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                mDownloadDBBean.setIsNew(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                mDownloadDBBean.setBtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                mDownloadDBBean.setEtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
                mDownloadDBBean.setDuration(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.DURATION)));
                arrayList.add(mDownloadDBBean);
            }
            return arrayList;
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }

    public DownloadDBBean getDownloadDBBeanPosition(int position) {
        Cursor cursor = null;
        DownloadDBBean mDownloadDBBean = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.FINISH + "<>?",
                    new String[] { "4" },
                    DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " ASC");
            if (cursor.move(position + 1)) {

                mDownloadDBBean = new DownloadDBBean();

                mDownloadDBBean.setVid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                mDownloadDBBean.setAid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                mDownloadDBBean.setIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                mDownloadDBBean.setType(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                mDownloadDBBean.setOrd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                mDownloadDBBean.setCid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                mDownloadDBBean.setEpisodeIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                mDownloadDBBean.setTotalsize(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                mDownloadDBBean.setFinish(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                mDownloadDBBean.setTimestamp(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                mDownloadDBBean.setLength(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                mDownloadDBBean.setFilePath(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                mDownloadDBBean.setIsHd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                mDownloadDBBean.setIsNew(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                mDownloadDBBean.setBtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                mDownloadDBBean.setEtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
            }

            return mDownloadDBBean;
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }

    /**
     * 是否存在本条记录
     * 
     * @param albumId
     * @param order
     * @return 存在记录的视频标题
     */
    public DownloadDBBean has(long episodeId) {
        Cursor cursor = null;
        DownloadDBBean mDownloadDBBean = null;
        try {

            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null,
                    DatabaseConstant.DownloadTrace.Field.EPISODE_ID + "=?",
                    new String[] { episodeId + "" }, null);

            if (cursor.moveToFirst()) {
                mDownloadDBBean = new DownloadDBBean();
                mDownloadDBBean.setVid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                mDownloadDBBean.setAid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                mDownloadDBBean.setIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                mDownloadDBBean.setType(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                mDownloadDBBean.setOrd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                mDownloadDBBean.setCid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                mDownloadDBBean.setEpisodeIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                mDownloadDBBean.setTotalsize(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                mDownloadDBBean.setFinish(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                mDownloadDBBean.setTimestamp(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                mDownloadDBBean.setLength(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                mDownloadDBBean.setFilePath(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                mDownloadDBBean.setIsHd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                mDownloadDBBean.setIsNew(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                mDownloadDBBean.setBtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                mDownloadDBBean.setEtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
                mDownloadDBBean.setDuration(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.DURATION)));
                return mDownloadDBBean;
            } else {
                return null;
            }

        } finally {
            LetvTools.closeCursor(cursor);
        }
    }

    /**
     * 获取下载表中所有的EpisodeId
     * 
     * @return
     */
    public List<Integer> getAllDownloadEpisodeId() {
        List<Integer> ids = new ArrayList<Integer>();
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    new String[] { DatabaseConstant.DownloadTrace.Field.EPISODE_ID }, null,
                    null, null);
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID));
                ids.add(id);
                // Constants.debug("episodeId:"+id);
            }
        } finally {
            LetvTools.closeCursor(cursor);
        }
        return ids;
    }

    /**
     * 查询所有的专辑ID，去重复
     * 
     * @return
     */
    public List<Long> getAllDownloadAlbumId() {
        List<Long> albumIds = null;
        Cursor cursor = null;
        try {
            String selection = 1 + " = " + 1 + " group by "
                    + DatabaseConstant.DownloadTrace.Field.ALBUM_ID;
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    new String[] { DatabaseConstant.DownloadTrace.Field.ALBUM_ID }, selection,
                    null, DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " ASC");
            albumIds = new ArrayList<Long>();
            while (cursor.moveToNext()) {
                long id = cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID));
                albumIds.add(id);
            }
            // if (albumIds != null && albumIds.size() > 0) {
            // HashSet<Long> albumIdsTmp = new HashSet<Long>(albumIds);
            // albumIds.clear();
            // albumIds.addAll(albumIdsTmp);
            // } else {
            // albumIds.clear();
            // albumIds = null;
            // }
        } finally {
            LetvTools.closeCursor(cursor);
        }
        return albumIds;
    }

    /**
     * 获取所有的下载信息 根据专辑ID分组
     * 
     * @return
     */
    // public LinkedHashMap<String, List<DownloadDBBean>> getAllDownLoadItems()
    // {
    // LinkedHashMap<String, List<DownloadDBBean>> mAllDownLoadItemsMap = null;
    // List<Long> albumIds = getAllDownloadAlbumId();
    // try {
    // mAllDownLoadItemsMap = new LinkedHashMap<String, List<DownloadDBBean>>();
    // if (albumIds != null && albumIds.size() > 0) {
    // for (Long albumId : albumIds) {
    // mAllDownLoadItemsMap.put(albumId + "",
    // getAllDownloadInfoWithAid(albumId));
    // }
    // }
    // mAllDownLoadItemsMap.put("videos", getAllDownloadInfoWithAid(0));//
    // 获取所有单视频列表
    // } finally {
    //
    // }
    // return mAllDownLoadItemsMap;
    // }
    public LinkedHashMap<String, List<DownloadDBBean>> getAllDownLoadItems() {
        LinkedHashMap<String, List<DownloadDBBean>> mAllDownLoadItemsMap = null;
        List<DownloadDBBean> mDownloadDBBeanList = null;
        try {
            List<Long> albumIds = getAllDownloadAlbumId();
            DownloadDBListBean allDownLoadTrace = getAllDownLoadTrace();
            mAllDownLoadItemsMap = new LinkedHashMap<String, List<DownloadDBBean>>();
            if (albumIds != null && albumIds.size() > 0) {
                if (allDownLoadTrace != null && allDownLoadTrace.size() > 0) {
                    for (Long albumId : albumIds) {
                        mDownloadDBBeanList = new ArrayList<DownloadDBBean>();
                        for (DownloadDBBean downloadDBBean : allDownLoadTrace) {
                            if (downloadDBBean.getAid() == albumId.longValue()) {
                                mDownloadDBBeanList.add(downloadDBBean);
                            }
                        }
                        if (mDownloadDBBeanList.size() > 0) {
                            mAllDownLoadItemsMap.put(albumId + "", mDownloadDBBeanList);
                        }
                    }
                }
            }
        } finally {
        }
        return mAllDownLoadItemsMap;
    }

    /**
     * 根据albumId与order获取已下载信息
     * 
     * @param albumId
     * @param order
     * @return 存在记录的视频标题
     */
    public DownloadDBBean getTitleInFinish(int albumId, float order) {
        synchronized (mContext) {
            Cursor cursor = null;
            try {
                DownloadDBBean mDownloadDBBean = null;
                cursor = mContext.getContentResolver().query(
                        LetvContentProvider.URI_DOWNLOADTRACE,
                        null,
                        DatabaseConstant.DownloadTrace.Field.ALBUM_ID + "=? AND "
                                + DatabaseConstant.DownloadTrace.Field.ORDER + "=? AND "
                                + DatabaseConstant.DownloadTrace.Field.FINISH + "=?",
                                new String[] { albumId + "", order + "", "4" }, null);
                if (cursor.moveToFirst()) {
                    mDownloadDBBean = new DownloadDBBean();
                    mDownloadDBBean.setVid(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                    mDownloadDBBean.setAid(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                    mDownloadDBBean.setIcon(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                    mDownloadDBBean.setType(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                    mDownloadDBBean.setOrd(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                    mDownloadDBBean.setCid(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                    mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                    mDownloadDBBean.setEpisodeIcon(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                    mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                    mDownloadDBBean.setTotalsize(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                    mDownloadDBBean.setFinish(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                    mDownloadDBBean.setTimestamp(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                    mDownloadDBBean.setLength(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                    mDownloadDBBean.setFilePath(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                    mDownloadDBBean.setIsHd(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                    mDownloadDBBean.setIsNew(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                    mDownloadDBBean.setBtime(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                    mDownloadDBBean.setEtime(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
                    mDownloadDBBean.setDuration(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.DURATION)));
                }
                return mDownloadDBBean;
            } finally {
                LetvTools.closeCursor(cursor);
            }
        }
    }

    /**
     * 根据视频ID 获取已下载信息
     * 
     * @param episodeId
     * @return
     */
    public DownloadDBBean getTitleInFinish(long episodeId) {
        synchronized (mContext) {
            Cursor cursor = null;
            try {
                DownloadDBBean mDownloadDBBean = null;
                cursor = mContext.getContentResolver().query(
                        LetvContentProvider.URI_DOWNLOADTRACE,
                        null,
                        DatabaseConstant.DownloadTrace.Field.EPISODE_ID + "=? AND "
                                + DatabaseConstant.DownloadTrace.Field.FINISH + "=?",
                                new String[] { episodeId + "", "4" }, null);
                if (cursor!= null && cursor.moveToFirst()) {
                    mDownloadDBBean = new DownloadDBBean();
                    mDownloadDBBean.setVid(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                    mDownloadDBBean.setAid(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                    mDownloadDBBean.setIcon(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                    mDownloadDBBean.setType(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                    mDownloadDBBean.setOrd(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                    mDownloadDBBean.setCid(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                    mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                    mDownloadDBBean.setEpisodeIcon(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                    mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                    mDownloadDBBean.setTotalsize(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                    mDownloadDBBean.setFinish(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                    mDownloadDBBean.setTimestamp(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                    mDownloadDBBean.setLength(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                    mDownloadDBBean.setFilePath(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                    mDownloadDBBean.setIsHd(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                    mDownloadDBBean.setIsNew(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                    mDownloadDBBean.setBtime(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                    mDownloadDBBean.setEtime(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
                    mDownloadDBBean.setDuration(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.DURATION)));
                }
                return mDownloadDBBean;
            } finally {
                LetvTools.closeCursor(cursor);
            }
        }
    }

    /**
     * 根据albumId与vid获取已下载信息
     * 
     * @param albumId
     * @param vid
     * @return 存在记录的视频标题
     */
    public DownloadDBBean getTitleInFinishByVid(int albumId, int vid) {
        synchronized (mContext) {
            Cursor cursor = null;
            try {
                DownloadDBBean mDownloadDBBean = null;
                cursor = mContext.getContentResolver().query(
                        LetvContentProvider.URI_DOWNLOADTRACE,
                        null,
                        DatabaseConstant.DownloadTrace.Field.ALBUM_ID + "=? AND "
                                + DatabaseConstant.DownloadTrace.Field.EPISODE_ID + "=? AND "
                                + DatabaseConstant.DownloadTrace.Field.FINISH + "=?",
                                new String[] { albumId + "", vid + "", "4" }, null);
                
                if (cursor.moveToFirst()) {
                    mDownloadDBBean = new DownloadDBBean();
                    mDownloadDBBean.setVid(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                    mDownloadDBBean.setAid(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                    mDownloadDBBean.setIcon(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                    mDownloadDBBean.setType(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                    mDownloadDBBean.setOrd(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                    mDownloadDBBean.setCid(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                    mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                    mDownloadDBBean.setEpisodeIcon(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                    mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                    mDownloadDBBean.setTotalsize(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                    mDownloadDBBean.setFinish(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                    mDownloadDBBean.setTimestamp(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                    mDownloadDBBean.setLength(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                    mDownloadDBBean.setFilePath(cursor.getString(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                    mDownloadDBBean.setIsHd(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                    mDownloadDBBean.setIsNew(cursor.getInt(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                    mDownloadDBBean.setBtime(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                    mDownloadDBBean.setEtime(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
                    mDownloadDBBean.setDuration(cursor.getLong(cursor
                            .getColumnIndex(DatabaseConstant.DownloadTrace.Field.DURATION)));
                }
                return mDownloadDBBean;
            } finally {
                LetvTools.closeCursor(cursor);
            }
        }
    }

    /**
     * 是否已经下载完成
     * 
     * @param episodeId
     * @return
     */
    public boolean isInFinish(String episodeId) {
        Cursor cursor = null;
        try {
            boolean isIn = false;

            cursor = mContext.getContentResolver().query(
                    LetvContentProvider.URI_DOWNLOADTRACE,
                    new String[] { DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE },
                    DatabaseConstant.DownloadTrace.Field.EPISODE_ID + "=? AND "
                            + DatabaseConstant.DownloadTrace.Field.FINISH + "=?",
                    new String[] { episodeId, "4" }, null);
            if (cursor.getCount() > 0) {
                isIn = true;
            }

            return isIn;
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }

    public void delete(final int episodeId) {
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                int delete = mContext.getContentResolver().delete(LetvContentProvider.URI_DOWNLOADTRACE,
                        DatabaseConstant.DownloadTrace.Field.EPISODE_ID + "=?",
                        new String[] { episodeId + "" });
            }
        });
        //LocationDownloadTraceManager.updataLocationTrace(mContext);
    }

    /**
     * 下载完成更新
     * 
     * @param episodeId
     * @param totalSize
     * @return
     */
    public boolean finish(int episodeId, long totalSize) {
        try {
            ContentValues cv = new ContentValues();
            cv.put(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE, totalSize);
            cv.put(DatabaseConstant.DownloadTrace.Field.LENGTH, totalSize);
            cv.put(DatabaseConstant.DownloadTrace.Field.FINISH, "4");
            mContext.getContentResolver().update(LetvContentProvider.URI_DOWNLOADTRACE, cv,
                    DatabaseConstant.DownloadTrace.Field.EPISODE_ID + "=?",
                    new String[] { episodeId + "" });
//          LetvFunction.updateDownLoadRate();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    
    private static class AsyDataBaseHandler extends AsyncQueryHandler{

        public AsyDataBaseHandler(ContentResolver cr) {
            super(cr);
        }
        
        @Override
        protected void onUpdateComplete(int token, Object cookie, int result) {
            super.onUpdateComplete(token, cookie, result);
        }
        
    }
    
    AsyDataBaseHandler asyDataBaseHandler;
    
    private AsyDataBaseHandler getAsyDataBaseHandler(){
        if (asyDataBaseHandler == null){
            asyDataBaseHandler = new AsyDataBaseHandler(mContext.getContentResolver());
        }
        return asyDataBaseHandler;
    }
    
    /**
     * 根据episodeId 更新下载大小
     * 
     * @param episodeId
     *            视频ID
     * @param totalSize
     *            总大小
     * @param downLoadSize
     *            已下载大小
     * @return
     */
    public boolean updateDownloadSize(String episodeId, long totalSize, long downLoadSize,
            int status) {
        try {
            ContentValues cv = new ContentValues();
            cv.put(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE, totalSize);
            cv.put(DatabaseConstant.DownloadTrace.Field.LENGTH, downLoadSize);
            cv.put(DatabaseConstant.DownloadTrace.Field.FINISH, status);
            getAsyDataBaseHandler().startUpdate(0, null, LetvContentProvider.URI_DOWNLOADTRACE, cv, DatabaseConstant.DownloadTrace.Field.EPISODE_ID + "=?", new String[] { episodeId });
//          context.getContentResolver().update(LetvContentProvider.URI_DOWNLOADTRACE, cv,
//                  DatabaseConstant.DownloadTrace.Field.EPISODE_ID + "=?",
//                  new String[] { episodeId });
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据aid 获取下载列表
     * 
     * @param albumId
     * @return
     */
    public List<DownloadDBBean> getAllDownloadInfoWithAid(long albumId) {
        Cursor cursor = null;
        List<DownloadDBBean> list = new ArrayList<DownloadDBBean>();
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.ALBUM_ID + "=?",
                    new String[] { albumId + "" },
                    DatabaseConstant.DownloadTrace.Field.TIMESTAMP + " ASC");
            DownloadDBBean mDownloadDBBean = null;
            while (cursor.moveToNext()) {
                mDownloadDBBean = new DownloadDBBean();
                mDownloadDBBean.setVid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ID)));
                mDownloadDBBean.setAid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_ID)));
                mDownloadDBBean.setIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ICON)));
                mDownloadDBBean.setType(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TYPE)));
                mDownloadDBBean.setOrd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ORDER)));
                mDownloadDBBean.setCid(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.CID)));
                mDownloadDBBean.setEpisodetitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_TITLE)));
                mDownloadDBBean.setEpisodeIcon(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.EPISODE_ICON)));
                mDownloadDBBean.setAlbumtitle(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ALBUM_TITLE)));
                mDownloadDBBean.setTotalsize(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TOTAL_SIZE)));
                mDownloadDBBean.setFinish(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FINISH)));
                mDownloadDBBean.setTimestamp(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.TIMESTAMP)));
                mDownloadDBBean.setLength(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.LENGTH)));
                mDownloadDBBean.setFilePath(cursor.getString(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.FILE_PATH)));
                mDownloadDBBean.setIsHd(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.ISHD)));
                mDownloadDBBean.setIsNew(cursor.getInt(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)));
                mDownloadDBBean.setBtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.B_TIME)));
                mDownloadDBBean.setEtime(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.E_TIME)));
                mDownloadDBBean.setDuration(cursor.getLong(cursor
                        .getColumnIndex(DatabaseConstant.DownloadTrace.Field.DURATION)));
                list.add(mDownloadDBBean);
            }
            return list;
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }
    /**
     * 根据专辑ID 判断是否存在记录
     * @param albumId
     * @return
     */
    public boolean hasOldVersionItem(long albumId) {
        Cursor cursor = null;
        try {
            cursor = mContext.getContentResolver().query(LetvContentProvider.URI_DOWNLOADTRACE,
                    null, DatabaseConstant.DownloadTrace.Field.ALBUM_ID + "=?",
                    new String[] { albumId + "" }, null);
            boolean isNew = false;
            while (cursor.moveToNext()) {
                if(cursor.getInt(cursor.getColumnIndex(DatabaseConstant.DownloadTrace.Field.IS_NEW)) == 0){
                    isNew = true;
                    break;
                }
            }
            return isNew;
        } finally {
            LetvTools.closeCursor(cursor);
        }
    }
    
    public void updateWatchStateByEpisodeid(int watchState, long episodeid) {
        try {
            ContentValues cv = new ContentValues();
            cv.put(DatabaseConstant.DownloadTrace.Field.IS_WATCH,
                    watchState);
            mContext.getContentResolver()
                    .update(LetvContentProvider.URI_DOWNLOADTRACE,
                            cv,
                            DatabaseConstant.DownloadTrace.Field.EPISODE_ID
                                    + "=?", new String[] { episodeid + "" });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void changeUserStatus(final DownloadDBBean downloadDBBean) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(has(downloadDBBean.getVid()) != null) {
//                  updateByEpisodeId(downloadDBBean);
                    updateStateByEpisodeId(downloadDBBean);
                }
            }           
        }).start();
    }
}
